//
//  constellation.swift
//  BirthdayManager
//
//  Created by Apple on 2020/12/16.
//  Copyright © 2020 Charles. All rights reserved.
//

import Foundation
import UIKit

class constellation :NSObject,NSCoding{
    //告诉编译器把内容存到哪里
    //参数：1.目录 2.mask
    static let userPath=NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first!+"/constellation.data"
    
    //解码（把object用一个对应的key保存）要持久化的内容
    //关键是保存对象的状态————键值对
    //后面使用哈希算法快速取出键值对中的内容
    func encode(with coder: NSCoder) {
        coder.encode(self.constellationName,forKey: "constellationNameKey")
        coder.encode(self.constellationType, forKey: "constellationTypeKey")
        coder.encode(self.constellationDescription, forKey: "constellationDescriptionKey")
        coder.encode(self.dateRange,forKey: "dateRangeKey")
        coder.encode(self.avatar,forKey: "avatarKey")
        coder.encode(self.poster,forKey: "posterKey")
        
    }
    
    //初始化这个对象时（new）decode出来
    //init的重载
    required init?(coder aDecoder: NSCoder) {
        constellationName=(aDecoder.decodeObject(forKey: "constellationNameKey") as? String)!
        constellationType=(aDecoder.decodeObject(forKey: "constellationTypeKey") as? String)!
        dateRange=(aDecoder.decodeObject(forKey: "dateRangeKey") as? String)!
        avatar=(aDecoder.decodeObject(forKey: "avatarKey") as? UIImage)
        constellationDescription=(aDecoder.decodeObject(forKey: "constellationDescriptionKey") as? String)!
        poster=(aDecoder.decodeObject(forKey:"posterKey")as? UIImage)
    }
    
    var constellationName: String?
    var dateRange: String?
    var constellationType: String?
    var constellationDescription: String?
    var avatar: UIImage?
    var poster: UIImage?
    
    init(constellationName: String, dateRange: String, constellationType: String, constellationDescription: String, avatar: UIImage?, poster:UIImage?) {
        self.constellationName = constellationName
        self.dateRange = dateRange
        self.constellationType = constellationType
        self.constellationDescription = constellationDescription
        self.avatar = avatar
        self.poster = poster
    }
}
